package com.devunion.reader.client;

import com.devunion.reader.client.core.AppViewport;
import com.devunion.reader.client.panel.AdvancedEastPanel;
import com.devunion.reader.client.panel.AdvancedWestPanel;
import com.devunion.reader.client.panel.ImageViewPanel;
import com.devunion.reader.client.panel.SimpleSearchPanel;
import com.devunion.reader.client.to.AuthorTo;
import com.devunion.reader.client.to.PaperInfoTo;
import com.devunion.reader.client.to.ProjectTreeTo;
import com.devunion.reader.client.to.SearchProjectTo;
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Theme;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Application implements EntryPoint {
    private AppViewport theViewport = new AppViewport();

    /*private MenuPanelMessageResources resources = (MenuPanelMessageResources) GWT.create(MenuPanelMessageResources.class);*/
    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        GXT.setDefaultTheme(Theme.GRAY, true);

        final SimpleSearchPanel projectSearchPanel = getSearchPanel();
        projectSearchPanel.initialize();
        theViewport.getPanel(Style.LayoutRegion.NORTH)
                .add(projectSearchPanel);

        init(projectSearchPanel);

        RootPanel.get().add(theViewport);
    }

    protected SimpleSearchPanel getSearchPanel() {
        return new SimpleSearchPanel();
    }

    protected void init(SimpleSearchPanel projectSearchPanel) {
        theViewport.clearAll();

        final AdvancedWestPanel westPanel = new AdvancedWestPanel();
        SearchProjectTo searchProjectTo = new SearchProjectTo();
        projectSearchPanel.bind(searchProjectTo);
        westPanel.getProjectPanel().getCmd().setSearchProjectTo(searchProjectTo);
        theViewport.getPanel(Style.LayoutRegion.WEST)
                .add(westPanel, new RowData(1d, 1d));

        final AdvancedEastPanel eastPanel = new AdvancedEastPanel();
        theViewport.getPanel(Style.LayoutRegion.EAST)
                .add(eastPanel, new RowData(1d, 1d));
        eastPanel.getProjectInfoPanel().layout();

        final ImageViewPanel imageViewPanel = new ImageViewPanel();
        imageViewPanel.initialize();
        theViewport.getPanel(Style.LayoutRegion.CENTER)
                .add(imageViewPanel, new RowData(1d, 1d));

        projectSearchPanel.getSearchButton().addSelectionListener(new SelectionListener<ButtonEvent>() {
            public void componentSelected(ButtonEvent buttonEvent) {
                westPanel.getProjectPanel().refresh();
            }
        });
        westPanel.getProjectPanel().getBinder().addSelectionChangedListener(new SelectionChangedListener<ProjectTreeTo>() {
            public void selectionChanged(SelectionChangedEvent<ProjectTreeTo> projectTreeToSelectionChangedEvent) {
                imageViewPanel.removeAll();

                ProjectTreeTo to = projectTreeToSelectionChangedEvent.getSelection().get(0);
                if (to.hasChildren() != null && to.hasChildren()) {
                    if (to.getPaperInfoTo() != null && to.getPaperInfoTo().getAuthor() != null) {
                        eastPanel.getProjectInfoPanel().bindAuthor(to.getPaperInfoTo().getAuthor());
                        eastPanel.getProjectInfoPanel().bindInfo(to.getPaperInfoTo());
                    } else if (to.getPaperInfoTo() != null) {
                        eastPanel.getProjectInfoPanel().bindAuthor(new AuthorTo());
                        eastPanel.getProjectInfoPanel().bindInfo(to.getPaperInfoTo());
                    } else {
                        eastPanel.getProjectInfoPanel().bindAuthor(new AuthorTo());
                        eastPanel.getProjectInfoPanel().bindInfo(new PaperInfoTo());
                    }
                } else {
                    imageViewPanel.setImagePath(to.getRelativePath());
                    imageViewPanel.setProjectName(to.getProjectName());
                    imageViewPanel.setAnnotations(to.getAnnotations());
                    imageViewPanel.build();
                    imageViewPanel.layout();
                }
            }
        });
    }

    protected AppViewport getViewport() {
        return theViewport;
    }
}
